﻿using System;
using System.Collections.Generic;
using olierFrm.GeneralCache;
using olierFrm.DataBase.FrameWork.Plugin;

namespace olierFrm.WebFormDev.Plugin
{
    [Serializable]
    public class ReceiptsConfigEntity
    {
        public const string ReceiptsConfigEntityCachePrefix = "ReceiptsConfigEntity=";

        public ReceiptsConfigEntity(string ModuleID)
        {
            string cacheKey = ReceiptsConfigEntityCachePrefix + ModuleID;
            if (!Cache.KeyExists(cacheKey))
            {
                var rc = new ReceiptsConfig(ModuleID);
                if (rc.IniScript.Trim().Length == 0) return;

                StdQueryEntity = new StdQuery();
                DataObjectEntity = new DataObject();
                BusinessEntity = new Business();
                FieldsDisplayEntity = new FieldsDisplay();
                SmartLookupEntity = new SmartLookup();
                ModuleInfoEntity = new ModuleInfo();

                StdQueryEntity.QueryName = rc.GetSection("STDQUERY")["QUERYNAME"];

                var doScript = rc.GetSection("DATAOBJECT");
                DataObjectEntity.ObjectName = doScript["OBJECTNAME"];

                var slScript = rc.GetSection("SMARTLOOKUP");
                var fdScript = rc.GetSection("FIELDS DISPLAY");
                var bScript = rc.GetSection("BUSINESS");
                var miScript = rc.GetSection("MODULE INFO");

                if (miScript.ContainsKey("模块信息"))
                {
                    var mi = miScript["模块信息"].Split('.');
                    ModuleInfoEntity.ModuleID = mi[0];
                    ModuleInfoEntity.ModuleName = mi[1];
                }
                ModuleInfoEntity.UpdateTime = miScript.ContainsKey("修改时间") ? miScript["修改时间"] : null;

                BusinessEntity.Add = bScript.ContainsKey("ADD") ? bScript["ADD"] : null;
                BusinessEntity.Modify = bScript.ContainsKey("MODIFY") ? bScript["MODIFY"] : null;
                BusinessEntity.Check = bScript.ContainsKey("CHECK") ? bScript["CHECK"] : null;
                BusinessEntity.Del = bScript.ContainsKey("DEL") ? bScript["DEL"] : null;
                SmartLookupEntity.Main = bScript.ContainsKey("MAIN") ? slScript["MAIN"] : null;

                DataObjectEntity.DetailLabel = new Dictionary<int, string>();
                SmartLookupEntity.Detail = new Dictionary<int, string>();

                FieldsDisplayEntity.SumFields = new Dictionary<int, string>();
                FieldsDisplayEntity.Display = new Dictionary<int, string>();
                FieldsDisplayEntity.ReadOnly = new Dictionary<int, string>();
                FieldsDisplayEntity.DicContext = new Dictionary<int, string>();
                FieldsDisplayEntity.Feedback = new Dictionary<int, string>();
                FieldsDisplayEntity.ReadOnlyOfEditState = new Dictionary<int, string>();
                for (int i = 0; i < 10; i++)
                {
                    if (doScript.ContainsKey("DETAILLABEL_" + i))
                        DataObjectEntity.DetailLabel.Add(i, doScript["DETAILLABEL_" + i]);
                    if (slScript.ContainsKey("DETAIL_" + i))
                        SmartLookupEntity.Detail.Add(i, slScript["DETAIL_" + i]);

                    if (fdScript.ContainsKey("SUMFIELDS_" + i))
                        FieldsDisplayEntity.SumFields.Add(i, fdScript["SUMFIELDS_" + i]);
                    if (fdScript.ContainsKey("DISPLAY_" + i))
                        FieldsDisplayEntity.Display.Add(i, fdScript["DISPLAY_" + i]);
                    if (fdScript.ContainsKey("READONLY_" + i))
                        FieldsDisplayEntity.ReadOnly.Add(i, fdScript["READONLY_" + i]);
                    if (fdScript.ContainsKey("DICCONTEXT_" + i))
                        FieldsDisplayEntity.DicContext.Add(i, fdScript["DICCONTEXT_" + i]);
                    if (fdScript.ContainsKey("FEEDBACK_" + i))
                        FieldsDisplayEntity.Feedback.Add(i, fdScript["FEEDBACK_" + i]);
                    if (fdScript.ContainsKey("READONLYOFEDITSTATE_" + i))
                        FieldsDisplayEntity.ReadOnlyOfEditState.Add(i, fdScript["READONLYOFEDITSTATE_" + i]);
                }
                Cache.Add(cacheKey, this);
                //DBFrm.FrameWork.Plugin.SmartINI.LoadIni(
            }
            else
            {
                var rce = (ReceiptsConfigEntity)Cache.Get(cacheKey);
                DataObjectEntity = rce.DataObjectEntity;
                BusinessEntity = rce.BusinessEntity;
                SmartLookupEntity = rce.SmartLookupEntity;
                FieldsDisplayEntity = rce.FieldsDisplayEntity;
                StdQueryEntity = rce.StdQueryEntity;
            }
        }

        public DataObject DataObjectEntity { get; private set; }
        public Business BusinessEntity { get; private set; }
        public SmartLookup SmartLookupEntity { get; private set; }
        public FieldsDisplay FieldsDisplayEntity { get; private set; }
        public StdQuery StdQueryEntity { get; private set; }
        public ModuleInfo ModuleInfoEntity { get; private set; }

        /// <summary>
        /// 脚本的模块信息
        /// </summary>
        public class ModuleInfo
        {
            public string ModuleID { get; set; }
            public string ModuleName { get; set; }
            public string UpdateTime { get; set; }
        }

        public class DataObject
        {
            public string ObjectName { get; set; }

            /// <summary>
            /// DetailLabel_1,DetailLabel_2,DetailLabel_3
            /// </summary>
            public Dictionary<int, string> DetailLabel { get; set; }
        }

        public class Business
        {
            public string Add { get; set; }
            public string Modify { get; set; }
            public string Del { get; set; }
            public string Check { get; set; }
        }

        public class SmartLookup
        {
            public string Main { get; set; }
            /// <summary>
            /// Detail_1,Detail_2,Detail_3
            /// </summary>
            public Dictionary<int, string> Detail { get; set; }
        }

        public class FieldsDisplay
        {
            /// <summary>
            /// SumFields_1,SumFields_2,SumFields_3
            /// </summary>
            public Dictionary<int, string> SumFields { get; set; }

            /// <summary>
            /// Display_0,Display_1,Display_2
            /// </summary>
            public Dictionary<int, string> Display { get; set; }

            /// <summary>
            /// ReadOnly_0,ReadOnly_1,ReadOnly_2
            /// </summary>
            public Dictionary<int, string> ReadOnly { get; set; }

            /// <summary>
            /// DicContext_0,DicContext_1,DicContext_2
            /// </summary>
            public Dictionary<int, string> DicContext { get; set; }

            /// <summary>
            /// Feedback_0,Feedback_1,Feedback_2
            /// </summary>
            public Dictionary<int, string> Feedback { get; set; }

            /// <summary>
            /// ReadOnlyOfEditState_0,ReadOnlyOfEditState_1,ReadOnlyOfEditState_2
            /// </summary>
            public Dictionary<int, string> ReadOnlyOfEditState { get; set; }
        }

        public class StdQuery
        {
            public string QueryName { get; set; }
        }
    }
}
